package com.ly.cyclictest.Test03;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;

public class Soldier implements Runnable {
    private String name;

   private final CyclicBarrier cyclicBarrier;

    public Soldier(String name, CyclicBarrier cyclicBarrier) {
        this.name = name;
        this.cyclicBarrier = cyclicBarrier;
    }


    @Override
    public void run() {
        try {
            cyclicBarrier.await();
            doo();
            cyclicBarrier.await();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void doo() {
        try {
            Thread.sleep(Math.abs(new Random().nextInt()%10000));
            System.out.println(name+"执行");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
